package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private JedisPool jedisPool;
    @Reference
    private MemberService memberService;

    @RequestMapping("/login")
    public Result login(@RequestBody Map map, HttpServletResponse response){
        String telephone = (String)map.get("telephone");
        String validateCode = (String) map.get("validateCode");
        //从redis中查出错误的次数,如果输入的次数超过2就无法登录
        String errorCount = jedisPool.getResource().get(telephone+"errorCount");
        if(errorCount!=null&&Integer.parseInt(errorCount)>=2){
            return new Result(false, "您已输入错误2次,请24小时以后再进行登录操作");
        }

        Result result=new Result(false,MessageConstant.VALIDATECODE_ERROR);
        try {
            String validateCode_redis = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
            if(validateCode!=""&&validateCode!=null&validateCode_redis!=null&&validateCode.equals(validateCode_redis)){
                //验证码验证正确
                result=new Result(true, MessageConstant.LOGIN_SUCCESS);
                 //通过手机号查询数据库是否是会员
                Member member=memberService.findByTelephone(telephone);
                if(member==null){
                    //不是会员的话自动完成注册
                    member = new Member();
                    member.setRegTime(new Date());
                    member.setPhoneNumber(telephone);
                    memberService.add(member);
                }
                //通过cookie将手机号保存到客户端
                Cookie cookie = new Cookie("login_member_telephone",telephone);
                cookie.setPath("/");
                cookie.setMaxAge(60*60*24*30);
                response.addCookie(cookie);
                //将会员信息保存到redis
                String json = JSON.toJSON(member).toString();
                jedisPool.getResource().setex(telephone,60*30,json);
            }else {
                //验证码错误
                result=new Result(false,MessageConstant.VALIDATECODE_ERROR);
                if(errorCount!=null){
                    //当redis中有errorCount时,更新errorCount
                    jedisPool.getResource().setex(telephone+"errorCount",2*60,Integer.parseInt(errorCount)+1+"");
                }else {
                    //当redis中没有errorCount是,新增errorCount
                    jedisPool.getResource().setex(telephone+"errorCount",2*60,"1");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new Result(false,MessageConstant.VALIDATECODE_ERROR);
        }
        return result;
    }

//    public static void main(String[] args) {
//        JedisPool jedisPool = (JedisPool) new ClassPathXmlApplicationContext("springmvc.xml").getBean("jedisPool");
//    }
}
